import axios from 'axios'

const api = axios.create({
  baseURL: '/api',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  }
})

// 响应拦截器
api.interceptors.response.use(
  response => {
    console.log('API Response:', {
      url: response.config.url,
      status: response.status,
      data: response.data
    })
    return response
  },
  error => {
    console.error('API Response Error:', {
      url: error.config?.url,
      status: error.response?.status,
      message: error.message,
      data: error.response?.data
    })
    
    let errorMessage = '请求失败'
    if (error.response) {
      switch (error.response.status) {
        case 404:
          errorMessage = '资源不存在'
          break
        case 500:
          errorMessage = '服务器错误'
          break
        default:
          errorMessage = error.response.data?.message || '未知错误'
      }
    } else if (error.request) {
      errorMessage = '网络错误，请检查网络连接'
    } else {
      error.message
    }
    
    return Promise.reject(new Error(errorMessage))
  }
)

// 请求拦截器
api.interceptors.request.use(
  config => {
    console.log('API Request:', {
      url: config.url,
      method: config.method,
      params: config.params,
      data: config.data
    })
    return config
  },
  error => {
    console.error('API Request Error:', error)
    return Promise.reject(error)
  }
)

export const getStats = async () => {
  try {
    const response = await api.get('/stats')
    return response.data
  } catch (error) {
    console.error('Error getting stats:', error)
    return {
      total_vulnerabilities: 0,
      by_severity: {
        high: 0,
        medium: 0,
        low: 0,
        critical: 0,
        fixed: 0
      }
    }
  }
}

export const getVulnerabilities = async (page = 1, pageSize = 10) => {
  try {
    const response = await api.get('/vulnerabilities', {
      params: { page, page_size: pageSize }
    })
    return response.data
  } catch (error) {
    console.error('Error getting vulnerabilities:', error)
    return {
      data: [],
      total: 0,
      page: 1,
      page_size: pageSize
    }
  }
}

export const getRecentVulnerabilities = async (page = 1, pageSize = 10) => {
  try {
    const response = await api.get('/recent-vulnerabilities', {
      params: { page, page_size: pageSize }
    })
    return response.data
  } catch (error) {
    console.error('Error getting recent vulnerabilities:', error)
    return {
      data: [],
      total: 0,
      page: 1,
      page_size: pageSize
    }
  }
}

export const searchVulnerabilities = async (query) => {
  try {
    const response = await api.get('/search', {
      params: { q: query }
    })
    return response.data
  } catch (error) {
    console.error('Error searching vulnerabilities:', error)
    return {
      data: [],
      total: 0
    }
  }
}

export const getSamples = async (vulnId) => {
  try {
    const response = await api.get(`/vulnerabilities/${vulnId}/samples`)
    return response.data
  } catch (error) {
    console.error('Error getting samples:', error)
    return []
  }
}

export default api 